

#import "QCClauseVC.h"
#import "MLLinkLabel.h"
#import "AppState.h"

#import "QCPrivacyPolicyVC.h"
#import "QCSubscriptionPolicyVC.h"
#import "QCTermsUseVC.h"

@interface QCClauseVC (){
    IBOutlet UIButton *_closeBtn;
    IBOutlet UIButton *_payBtn;
    
    IBOutlet UILabel *_proLabel;
    
    IBOutlet UILabel *_priceLabel;
    IBOutlet MLLinkLabel *_clauseLabel;
    IBOutlet UILabel*_explainLabel;
    IBOutlet UILabel*_titleLabel;
    IBOutlet UILabel*_confirmLabel;
    IBOutlet UILabel*_jioningLabel;
    
    IBOutlet UIScrollView *_containerView;
}
@end

@implementation QCClauseVC

-(void)beforehandText
{
    NSMutableAttributedString *clauseAttString = [[NSMutableAttributedString alloc] initWithString:_clauseLabel.text];
    [clauseAttString setAttributes:@{NSLinkAttributeName : @"Terms of Use",NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:[_clauseLabel.text rangeOfString:@"Terms of Use"]];
    [clauseAttString setAttributes:@{NSLinkAttributeName : @"Privacy Policy",NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:[_clauseLabel.text rangeOfString:@"Privacy Policy"]];
    [clauseAttString setAttributes:@{NSLinkAttributeName : @"Subscription Policy",NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:[_clauseLabel.text rangeOfString:@"Subscription Policy"]];
    _clauseLabel.attributedText = clauseAttString;
    
    weakify(self)
    [_clauseLabel setDidClickLinkBlock:^(MLLink *link, NSString *linkText, MLLinkLabel *label){
        strongify(self)
        if([linkText isEqualToString:@"Terms of Use"]){
            [self attributedStringAction:0];
        }else if([linkText isEqualToString:@"Privacy Policy"]){
            [self attributedStringAction:1];
        }else if([linkText isEqualToString:@"Subscription Policy"]){
            [self attributedStringAction:2];
        }
    }];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [_containerView setContentSize:CGSizeMake(CC_SCREEN_WIDTH, 600.0f)];
//    [SVProgressHUD dismiss];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self beforehandText];
        
    if(CC_APP_STATE_R == AppStateInstance.versionCode || AppStateInstance.versionCode == CC_APP_STATE_INIT){
        [_payBtn setTitle:@"Try for Free ($0.99/week)" forState:UIControlStateNormal];
    }
    _payBtn.layer.masksToBounds = YES;
    _payBtn.layer.cornerRadius = 29.0f;
    
    _proLabel.layer.masksToBounds = YES;
    _proLabel.layer.cornerRadius = 12.0f;
    
    [self.view insertSubview:_containerView belowSubview:_payBtn];
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_closeBtn.mas_bottom).mas_offset(10.0f);
        make.left.right.bottom.mas_equalTo(self.view).mas_offset(0.0f);
    }];
    
    _titleLabel.text = [_titleLabel.text stringByReplacingOccurrencesOfString:@"app_name" withString:kCCFBDisplayName];
    _priceLabel.text = [_priceLabel.text stringByReplacingOccurrencesOfString:@"app_name" withString:kCCFBDisplayName];
    _jioningLabel.text = [_jioningLabel.text stringByReplacingOccurrencesOfString:@"app_name" withString:kCCFBDisplayName];
    _confirmLabel.text = [_confirmLabel.text stringByReplacingOccurrencesOfString:@"app_name" withString:kCCFBDisplayName];
}


#pragma mark - AttributedString Event
-(void)attributedStringAction:(NSInteger)index
{
    UIViewController *viewController = nil;
    
    if(0 == index){
        viewController = [[QCTermsUseVC alloc] initWithNibName:@"QCTermsUseVC" bundle:nil];
    }else if(1 == index){
        viewController = [[QCPrivacyPolicyVC alloc] initWithNibName:@"QCPrivacyPolicyVC" bundle:nil];
    }else if(2 == index){
        viewController = [[QCSubscriptionPolicyVC alloc] initWithNibName:@"QCSubscriptionPolicyVC" bundle:nil];
    }
    
    if(viewController != nil){
        [self.navigationController pushViewController:viewController animated:YES];
    }
}

#pragma mark - button action
-(IBAction)onRestore:(UIButton *)button
{
    //do something ...
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD dismissWithDelay:5];
    [[SRSubscriptionModel shareKit] restoreSubscriptions];
}

-(IBAction)onClose:(UIButton *)button
{
    [self.navigationController  popViewControllerAnimated:YES];
}

-(IBAction)onPayforFree:(UIButton *)button
{
    //审核版 需要能看到条款
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD dismissWithDelay:5];
    [[SRSubscriptionModel shareKit] makePurchase:SRSubscription];
}

@end
